﻿Public Partial Class Wfrm_Paso3
    Inherits System.Web.UI.Page
    Dim Dt1 As New DataTable
    Dim Dt2 As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("ptd_rut") Is Nothing Then
            Response.Redirect("../default.aspx")
        End If

        If Not Page.IsPostBack Then
            ViewState("prox_hora") = Session("prox_hora")
            ViewState("centro_medico") = Session("centro_medico")
            ViewState("profesional") = Session("profesional")

            ViewState("ptd_rut") = Session("ptd_rut")
            ViewState("agp_correl") = Session("agp_correl")
            ViewState("age_correl") = Session("age_correl")
            ViewState("fecha") = Session("fecha")
            ViewState("hora") = Session("hora")
            ViewState("especialidad_texto") = Session("especialidad_texto")
            ViewState("especialidad_codigo") = Session("especialidad_codigo")

            ViewState("default") = Session("default")


            Dim fec As DateTime
            fec = CDate(ViewState("prox_hora")).AddMonths(1)
            lbl_mes1.Text = "Horas Disponibles para  "
            lbl_mes3.Text = DotNetDal.Generica.GetMes(CDate(ViewState("prox_hora")).Month) & " De " & CDate(ViewState("prox_hora")).Year

            lbl_mes2.Text = "Horas Disponibles para  "
            lbl_mes4.Text = DotNetDal.Generica.GetMes(fec.Month) & " De " & fec.Year

            Carga_Calendario_Medico()
            Carga_Calendario_Detalle(Mid(Session("prox_hora").ToString, 1, 10))

            If Not Session("txt_mensaje") Is Nothing Then
                If Session("txt_mensaje") = "Y" Then
                    Grid_Horas.SelectedIndex = 0
                    Grid_Horas_SelectedIndexChanged(Nothing, Nothing)
                End If
            End If


            CreaCalendario1()
            CreaCalendario2()
        End If

    End Sub
    Sub Carga_Calendario_Detalle(ByVal Fecha_Siguiente As String)
        ' ViewState("fecha_siguiente") = "25-03-2011"
        ' ViewState("hora_siguiente") = "09:00"

        lbl_fecha_detalle.Text = Fecha_Siguiente.Replace(" 0:00:00", "")
        lbl_fecha_detalle.UpdateAfterCallBack = True

        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_AME_GET_CALENDARIO_MEDICO"
        rp.AddParametro(ViewState("ptd_rut"))
        rp.AddParametro(lbl_fecha_detalle.Text)
        exec.ExecuteDataTable(rp)
        If rp.TieneDatos Then
            Dim Row1() As DataRow
            Dim m1 As String = CDate(lbl_fecha_detalle.Text).Month
            Dim a1 As String = CDate(lbl_fecha_detalle.Text).Year

            Dim Dt1 As New DataTable
            Dt1.Columns.Add("EspNombre")
            Dt1.Columns.Add("Hora")
            Dt1.Columns.Add("PTD_RUT")
            Dt1.Columns.Add("AGP_CORREL")
            Dt1.Columns.Add("AGE_CORREL")

            Row1 = rp.DtTable.Select("fecha='" & Fecha_Siguiente.Replace(" 0:00:00", "") & "'")
            Dim i As Integer
            For i = 0 To Row1.Count - 1
                Dim dr As DataRow
                dr = Dt1.NewRow
                dr(0) = Row1(i).Item("EspNombre")
                dr(1) = Row1(i).Item("Hora")
                dr(2) = Row1(i).Item("PTD_RUT")
                dr(3) = Row1(i).Item("AGP_CORREL")
                dr(4) = Row1(i).Item("AGE_CORREL")

                Dt1.Rows.Add(dr)
            Next

            Grid_Horas.DataSource = Dt1
            Grid_Horas.DataBind()
            Grid_Horas.UpdateAfterCallBack = True

            ' recorrer la grilla y asignar a cada imagen reservar la fila de la grilla, para luego sacar los valores
            'For i = 0 To Grid_Horas.Items.Count - 1
            '    DirectCast(Grid_Horas.Items(i).Cells(2).Controls(0), Anthem.ImageButton).CommandArgument = i
            '    DirectCast(Grid_Horas.Items(i).Cells(2).Controls(0), Anthem.ImageButton).UpdateAfterCallBack = True
            'Next
        End If

    End Sub
    Sub Carga_Calendario_Medico()
        Dim i As Integer
        Dim fecha As Date = Mid(Session("prox_hora").ToString, 1, 10)


        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_AME_GET_CALENDARIO_MEDICO"
        rp.AddParametro(ViewState("ptd_rut"))
        rp.AddParametro(fecha)
        exec.ExecuteDataTable(rp)
        If rp.TieneDatos Then
            Dim Row1() As DataRow
            Dim m1 As String = fecha.Month
            Dim a1 As String = fecha.Year
            Dt1.Columns.Add("fecha")
            Dt1.Columns("fecha").Unique = True
            Row1 = rp.DtTable.Select("Mes_Anio='" & m1 & "/" & a1 & "'")
            For i = 0 To Row1.Count - 1
                Dim fec As DateTime = Row1(i).Item("fecha")
                Dim dia_nombre As String = DotNetDal.Generica.GetDayName(fec.DayOfWeek)
                Dim dia As String = fec.Day
                If dia.Length = 1 Then dia = "0" & dia
                Dim anio As String = fec.Year
                Dim dr As DataRow
                dr = Dt1.NewRow
                Try
                    dr("fecha") = dia & "/" & (fec.Month) & "/" & fec.Year
                    Dt1.Rows.Add(dr)
                Catch ex As Exception

                End Try
            Next
            'Grid_Fechas1.DataSource = Dt1
            'Grid_Fechas1.DataBind()
            'Grid_Fechas1.UpdateAfterCallBack = True




            Dim Row2() As DataRow
            Dim fec2 As New DateTime
            Dim m2 As String = fecha.AddMonths(1).Month
            Dim a2 As String = fecha.AddMonths(1).Year
            Dt2.Columns.Add("fecha")
            Dt2.Columns("fecha").Unique = True
            Row2 = rp.DtTable.Select("Mes_Anio='" & m2 & "/" & a2 & "'")
            For i = 0 To Row2.Count - 1
                Dim fec As DateTime = Row2(i).Item("fecha")
                Dim dia_nombre As String = DotNetDal.Generica.GetDayName(fec.DayOfWeek)
                Dim dia As String = fec.Day
                If dia.Length = 1 Then dia = "0" & dia
                Dim anio As String = fec.Year
                Dim dr As DataRow
                dr = Dt2.NewRow
                Try
                    dr("fecha") = dia & "/" & (fec.Month) & "/" & fec.Year
                    Dt2.Rows.Add(dr)
                Catch ex As Exception

                End Try
            Next
            'Grid_Fechas2.DataSource = Dt2
            'Grid_Fechas2.DataBind()
            'Grid_Fechas2.UpdateAfterCallBack = True


        End If
        If rp.Errores Then
            'envío mensaje de error
        End If


    End Sub


    Sub CreaCalendario1()
        Dim DIAS() As String = {"L", "M", "MI", "J", "V", "S", "D"}
        Dim dia As Integer
        Dim FECHA As Date
        Dim fec0 As DateTime = Mid(Session("prox_hora").ToString, 1, 10)

        FECHA = "01-" & IIf(fec0.Month.ToString.Length = 1, "0" & fec0.Month, fec0.Month) & "-" & fec0.Year     '"01-03-2011"

        Select Case FECHA.DayOfWeek
            Case DayOfWeek.Monday : dia = 0
            Case DayOfWeek.Tuesday : dia = 1
            Case DayOfWeek.Wednesday : dia = 2
            Case DayOfWeek.Thursday : dia = 3
            Case DayOfWeek.Friday : dia = 4
            Case DayOfWeek.Saturday : dia = 5
            Case DayOfWeek.Sunday : dia = 6
        End Select


        Dim DIAFINAL As String = CDate("01-" & FECHA.AddMonths(1).Month & "-" & FECHA.AddMonths(1).Year).AddDays(-1).Day
        Dim I As Integer
        Dim di As String = 1
        Dim mes As String = FECHA.Month
        Dim anio As String = FECHA.Year
        If di.Length = 1 Then di = "0" & di
        If mes.Length = 1 Then mes = "0" & mes
        Dim fec As Date = CDate(di & "-" & mes & "-" & anio)
        Dim k As Integer
        Dim corr As Integer = 0
        For I = 1 To DIAFINAL
            Dim j As Integer
            k = dia
            corr = corr + 1
            For j = 0 To 6
                Dim ctl As Anthem.Button
                ctl = Me.FindControl("C1" & DIAS(k) & corr)
                ctl.Text = IIf(fec.Day.ToString.Length = 1, "0" & fec.Day.ToString, fec.Day.ToString)
                ctl.Visible = True
                ctl.EnabledDuringCallBack = False
                ctl.EnableCallBack = True

                ctl.CommandArgument = fec.ToString
                ctl.Enabled = False
                Dim l As Integer
                For l = 0 To Dt1.Rows.Count - 1
                    If fec <> CDate(Dt1.Rows(l).Item("fecha")) Then
                        ctl.Enabled = False
                        ctl.BackColor = System.Drawing.ColorTranslator.FromHtml("#DADADA")
                    Else
                        ctl.Enabled = True
                        ctl.Font.Bold = True
                        ' ctl.BackColor = Drawing.Color.blue
                        ctl.BackColor = System.Drawing.ColorTranslator.FromHtml("#004C92")
                        ctl.ForeColor = Drawing.Color.White
                        Exit For
                    End If
                Next

                ctl.UpdateAfterCallBack = True
                If fec.Day = DIAFINAL Then
                    j = 6
                    I = DIAFINAL
                End If
                fec = fec.AddDays(1)
                If k = 6 Then
                    j = k
                End If
                k = k + 1
            Next
            dia = 0
        Next



    End Sub


    Sub CreaCalendario2()
        Dim DIAS() As String = {"L", "M", "MI", "J", "V", "S", "D"}
        Dim dia As Integer
        Dim FECHA As Date

        Dim fec0 As DateTime = Mid(Session("prox_hora").ToString, 1, 10)

        FECHA = "01-" & IIf(fec0.AddMonths(1).Month.ToString.Length = 1, "0" & fec0.AddMonths(1).Month, fec0.AddMonths(1).Month) & "-" & fec0.AddMonths(1).Year     '"01-03-2011"

        Select Case FECHA.DayOfWeek
            Case DayOfWeek.Monday : dia = 0
            Case DayOfWeek.Tuesday : dia = 1
            Case DayOfWeek.Wednesday : dia = 2
            Case DayOfWeek.Thursday : dia = 3
            Case DayOfWeek.Friday : dia = 4
            Case DayOfWeek.Saturday : dia = 5
            Case DayOfWeek.Sunday : dia = 6
        End Select


        Dim DIAFINAL As String = CDate("01-" & FECHA.AddMonths(1).Month & "-" & FECHA.AddMonths(1).Year).AddDays(-1).Day
        Dim I As Integer
        Dim di As String = 1
        Dim mes As String = FECHA.Month
        Dim anio As String = FECHA.Year
        If di.Length = 1 Then di = "0" & di
        If mes.Length = 1 Then mes = "0" & mes
        Dim fec As Date = CDate(di & "-" & mes & "-" & anio)
        Dim k As Integer
        Dim corr As Integer = 6
        For I = 1 To DIAFINAL
            Dim j As Integer
            k = dia
            corr = corr + 1
            For j = 0 To 6
                Dim ctl As Anthem.Button
                ctl = Me.FindControl("C1" & DIAS(k) & corr)
                ctl.Text = IIf(fec.Day.ToString.Length = 1, "0" & fec.Day.ToString, fec.Day.ToString)
                ctl.Visible = True
                ctl.EnabledDuringCallBack = False
                ctl.EnableCallBack = True

                ctl.CommandArgument = fec.ToString
                ctl.Enabled = False

                Dim l As Integer
                For l = 0 To Dt2.Rows.Count - 1
                    If fec <> CDate(Dt2.Rows(l).Item("fecha")) Then
                        ctl.Enabled = False
                        ctl.BackColor = System.Drawing.ColorTranslator.FromHtml("#DADADA")
                    Else
                        ctl.Enabled = True
                        ctl.Font.Bold = True
                        ctl.BackColor = System.Drawing.ColorTranslator.FromHtml("#004C92")
                        ctl.ForeColor = Drawing.Color.White
                        Exit For
                    End If
                Next

                ctl.UpdateAfterCallBack = True
                If fec.Day = DIAFINAL Then
                    j = 6
                    I = DIAFINAL
                End If
                fec = fec.AddDays(1)
                If k = 6 Then
                    j = k
                End If
                k = k + 1
            Next
            dia = 0
        Next



    End Sub
 

    Protected Sub Calendar_Click(ByVal sender As Object, ByVal e As EventArgs)
        Carga_Calendario_Detalle(DirectCast(sender, Anthem.Button).CommandArgument)
    End Sub

  
    Protected Sub Grid_Horas_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Grid_Horas.SelectedIndexChanged
        '3,4,5
        If ViewState("default") = Nothing Then
            Session("prox_hora") = ViewState("prox_hora")
        Else
            Session("prox_hora") = lbl_fecha_detalle.Text & " " & Grid_Horas.SelectedItem.Cells(1).Text
        End If

        Session("centro_medico") = ViewState("centro_medico")
        Session("profesional") = ViewState("profesional")

        Session("ptd_rut") = Grid_Horas.SelectedItem.Cells(3).Text
        Session("agp_correl") = Grid_Horas.SelectedItem.Cells(4).Text
        Session("age_correl") = Grid_Horas.SelectedItem.Cells(5).Text
        Session("fecha") = Me.lbl_fecha_detalle.Text
        Session("hora") = Grid_Horas.SelectedItem.Cells(1).Text
        Session("especialidad_texto") = Grid_Horas.SelectedItem.Cells(0).Text          
        Session("especialidad_codigo") = ViewState("especialidad_codigo")

        Response.Redirect("Wfrm_Paso4.aspx")
    End Sub

    'Protected Sub Img_Ir_Especialidad_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
    '    Session("prox_hora") = ViewState("prox_hora")
    '    Session("centro_medico") = ViewState("centro_medico")
    '    Session("profesional") = ViewState("profesional")

    '    Session("ptd_rut") = Grid_Horas.SelectedItem.Cells(3).Text
    '    Session("agp_correl") = Grid_Horas.SelectedItem.Cells(4).Text
    '    Session("age_correl") = Grid_Horas.SelectedItem.Cells(5).Text
    '    Session("fecha") = Me.lbl_fecha_detalle.Text
    '    Session("hora") = Grid_Horas.SelectedItem.Cells(1).Text
    '    Session("especialidad_texto") = Grid_Horas.SelectedItem.Cells(0).Text
    '    Session("especialidad_codigo") = ViewState("especialidad_codigo")

    '    Response.Redirect("Wfrm_Paso4.aspx")
    'End Sub
End Class